
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<link rel=stylesheet href='include/hoj.css' type='text/css'>
</head>
<body>
<center>
<div style="width:90%; text-align:left">
<img src="image/logo.png"/>
</div>
<table width=96%> 
	<tr align="center" class='hd' valign="top">
				<th><a href="faqs.php">F.A.Qs</a></th>
		<th><a href="./bbs.php">Web Board</a></th>
		<th><a href="./">Home</a></th>
		<th><a href="./problemset.html">ProblemSet</a></th>
		<th><a href="./status.php">Status</a></th>
		<th><a href="./ranklist.php">Ranklist</a></th>
		<th><a href="./contest.php">Contest</a></th>
		<th><a href=loginpage.php>Login</a></th><th><a href=registerpage.php>Register</a></th>	</tr>
</table>
</center>
<center>
<div class="notice">
	<div>
		<B>Notice:</B>鉴于种种原因，本OJ自下周星期一（3月5号）开始不再全面开放，请各位做好善后事宜，谢谢合作。	</div>
</div>
</center>
</div>
<title>Problem 2360. -- 唯美村落 -- 衡阳八中OJ离线版-2012-02-29</title><center><h2>2360: 唯美村落</h2><span class=green>Time Limit: </span>10 Sec&nbsp;&nbsp;<span class=green>Memory Limit: </span>128 MB<br><span class=green>Submit: </span>13&nbsp;&nbsp;<span class=green>Solved: </span>8<br>[<a href='submitpage.php?id=2360'>Submit</a>][<a href='problemstatus.php?id=2360'>Status</a>][<a href='bbs.php?id=2360'>Discuss</a>]</center><h2>Description</h2><div class=content><p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 24.1pt; line-height: 200%; mso-layout-grid-align: none"><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">很久以前，有一个美丽的村落，村落由</span><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">n</span><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">间房屋和</span><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">m</span><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">条道路构成，每个房子连出的边都不会指向它自身。道路可能是单向的，也可能是双向的，两个房子之间不会有多条道路。如果从房屋</span><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">a</span><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">仅走一条道路可以到达房屋</span><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">b</span><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">，则称</span><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">a--&gt;b.<o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 24.1pt; line-height: 200%; mso-layout-grid-align: none"><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">这个村落的道路遵循着如下&ldquo;唯美&rdquo;的规律：</span><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 24.1pt; line-height: 200%; mso-layout-grid-align: none"><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">若</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">a--&gt;b</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">且</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">a--&gt;c</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">，则</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">b</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">、</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">c</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">之间一定无道路，且如果</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">d--&gt;b</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">，那么，一定有</span></b><b style="mso-bidi-font-weight: normal"><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">d--&gt;c.<o:p></o:p></span></b></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 24.1pt; line-height: 200%; mso-layout-grid-align: none"><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">由此，这个村落就有了&ldquo;唯美村落&rdquo;之称，某一天，小</span><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">P</span><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">想从某个房屋出发，沿着道路拜访每个房屋恰一次，最后回到开始的房屋。小</span><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">P</span><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">知道这是经典的</span><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN">Hamilton</span><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">回路问题，一般人在多项式时间内是解决不了的，但他相信你能解决。另外，</span><span style="font-size: 12pt; line-height: 200%; font-family: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">小鹏提出了更高的要求，他给每个房屋定了一个重要值，各房屋的重要程度互不相同，他将把起点选在最重要的房屋上，然后尽量先访问重要值大的房屋，即要求依次访问的房屋的重要值组成的序列的<b style="mso-bidi-font-weight: normal">字典序</b>尽量大。</span><span style="font-size: 12pt; line-height: 200%; font-family: &quot;Courier New&quot;; mso-ansi-language: ZH-CN"><o:p></o:p></span></p>
<p></p></div><h2>Input</h2><div class=content><p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 24.1pt; mso-layout-grid-align: none"><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">第一行包含两个正整数</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">n</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">，</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">m</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">。</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 24.1pt; mso-layout-grid-align: none"><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">第二行包含</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">n</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">个用空格隔开整数，分别表示每个房屋的重要值。</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 24.1pt; mso-layout-grid-align: none"><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">接下来</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">m</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">行，每行三个正整数</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">a,b,c</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">，表示一条边，</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">c=1</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">代表</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">a</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">连向</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">b</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">，</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">c=2</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">代表这是一条双向边。</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 24.1pt; mso-layout-grid-align: none"><span lang="EN-US" style="font-size: 12pt; font-family: 'Courier New'"><span style="mso-bidi-font-style: italic"><o:p></o:p></span></span></p></div><h2>Output</h2><div class=content><p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 24.1pt; mso-layout-grid-align: none"><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">如果原图不存在</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">Hamilton</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">回路，则输出</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">-1</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">；否则输出</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">n</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">行，为</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">1~n</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">的一个排列，即最佳的</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN">Hamilton</span><span style="font-size: 12pt; font-family: 宋体; mso-ascii-font-family: 'Courier New'; mso-hansi-font-family: 'Courier New'; mso-bidi-font-family: 'Courier New'; mso-ansi-language: ZH-CN">回路，排列的第一个数为起点，最后一个数实际上是连向起点的。</span><span style="font-size: 12pt; font-family: 'Courier New'; mso-ansi-language: ZH-CN"><o:p></o:p></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 24pt; mso-layout-grid-align: none; mso-char-indent-count: 2.0"><span lang="EN-US" style="font-size: 12pt; font-family: 'Courier New'"><span style="mso-bidi-font-style: italic"><o:p></o:p></span></span></p></div><h2>Sample Input</h2>
			<div class=content><span class=sampledata>5 7<br />
<br />
50 40 30 20 10<br />
<br />
1 3 2<br />
<br />
1 4 2<br />
<br />
2 3 1<br />
<br />
2 4 1<br />
<br />
3 5 1<br />
<br />
4 5 1<br />
<br />
5 2 1<br />
<br />
</span></div><h2>Sample Output</h2>
			<div class=content><span class=sampledata><br />
1<br />
<br />
3<br />
<br />
5<br />
<br />
2<br />
<br />
4<br />
<br />
</span></div><h2>HINT</h2>
			<div class=content><p><p>【数据规模】</p><br />
<p>30%的数据满足，n&lt;=100，m&lt;=1000；</p><br />
<p>100%的数据满足，3&lt;=n&lt;=20000，m&lt;=500000；题给的都是&ldquo;唯美村落&rdquo;.<br /><br />
</p></p></div><h2>Source</h2>
			<div class=content><p><a href='problemset.html?search='></a></p></div><center>[<a href='submitpage.php?id=2360'>Submit</a>][<a href='problemstatus.php?id=2360'>Status</a>][<a href='bbs.php?id=2360'>Discuss</a>]</center>﻿<br>

<a href="./"><span class=red>HOME</span></a>
<a href="javascript:history.go(-1)"><span class=red>Back</span></a>

<hr>
<center>
	<div class="footer">
			<a href=setlang.php?lang=ko>한국어</a>&nbsp;
		<a href=setlang.php?lang=cn>中文</a>&nbsp;
		<a href=setlang.php?lang=fa>فارسی</a>&nbsp;
		<a href=setlang.php?lang=en>English</a>&nbsp;
		<a href=setlang.php?lang=th>ไทย</a>
	<br>		<div>版权所有 &copy;2008-2012 WaterPark Organization. | <script src="http://s21.cnzz.com/stat.php?id=2982771&web_id=2982771" language="JavaScript"></script>
</div>
		<div>Based on opensource project <a href="http://hustoj.googlecode.com">hustoj</a>.</div>
	</div>
</center>
</body>
</html>
